<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="药品名称" prop="drugName">
        <el-input
          v-model="queryParams.drugName"
          placeholder="请输入药品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商" prop="provider">
        <el-input
          v-model="queryParams.provider"
          placeholder="请输入供应商"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="批次" prop="batch">
        <el-input
          v-model="queryParams.batch"
          placeholder="请输入批次"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="价格" prop="price">
        <el-input
          v-model="queryParams.price"
          placeholder="请输入价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格" prop="scale">
        <el-input
          v-model="queryParams.scale"
          placeholder="请输入规格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="告警阈值" prop="margin">
        <el-input
          v-model="queryParams.margin"
          placeholder="请输入告警阈值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="库存" prop="store">
        <el-input
          v-model="queryParams.store"
          placeholder="请输入库存"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="药品类型" prop="store">
        <el-input
            v-model="queryParams.drugType"
            placeholder="请输入药品类型"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="drugList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商品编码" align="center" prop="id" />
      <el-table-column label="药名" align="center" prop="drugName" />
      <el-table-column label="供应商" align="center" prop="provider" />
      <el-table-column label="批次" align="center" prop="batch" />
      <el-table-column label="价格" align="center" prop="price" />
      <el-table-column label="规模" align="center" prop="scale" />
      <el-table-column label="告警阈值" align="center" prop="margin" />
      <el-table-column label="库存" align="center" prop="store" />
      <el-table-column label="生产日期" align="center" prop="produceDatetime" >
      </el-table-column>
      <el-table-column label="保质期" align="center" prop="expireTime" />
      <el-table-column label="药品类型" align="center" prop="drugType" />
      <el-table-column label="生产地址" align="center" prop="location" />
      <el-table-column label="药品描述" align="center" prop="drugDesc" />
      <el-table-column label="注意事项" align="center" prop="remark" />
      <el-table-column label="药品功效" align="center" prop="func" />
      <el-table-column label="所治疾病" align="center" prop="disease" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" >修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body >
      <el-form ref="drugRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="药品名称" prop="drugName">
          <el-input v-model="form.drugName" placeholder="请输入药品名称" />
        </el-form-item>
        <el-form-item label="供应商" prop="provider">
          <el-input v-model="form.provider" placeholder="请输入供应商" />
        </el-form-item>
        <el-form-item label="批次" prop="batch">
          <el-input v-model="form.batch" placeholder="请输入批次" />
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入价格" />
        </el-form-item>
        <el-form-item label="规格" prop="scale">
          <el-input v-model="form.scale" placeholder="请输入规格" />
        </el-form-item>
        <el-form-item label="告警阈值" prop="margin">
          <el-input v-model="form.margin" placeholder="请输入告警阈值" />
        </el-form-item>
        <el-form-item label="库存" prop="store">
          <el-input v-model="form.store" placeholder="请输入库存" />
        </el-form-item>
        <el-form-item label="生产日期" prop="produceDatetime">
          <el-date-picker clearable
                          v-model="form.produceDatetime"
                          type="date"
                          value-format="YYYY-MM-DD"
                          placeholder="请选择生产日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="保质期" prop="expireTime">
          <el-input v-model="form.expireTime" placeholder="请输入保质期" />
        </el-form-item>
        <el-form-item label="生产地址" prop="location">
          <el-input v-model="form.location" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="药品描述" prop="drugDesc">
          <el-input v-model="form.drugDesc" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="药品类型" prop="drugDesc">
          <el-input v-model="form.drugType" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="注意事项" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="药品功效" prop="func">
          <el-input v-model="form.func" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="疾病" prop="disease">
          <el-input v-model="form.disease" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script setup >
import { listDrug, getDrug, delDrug, addDrug, updateDrug } from "@/api/system/drug";

const { proxy } = getCurrentInstance();

const drugList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    drugName: null,
    provider: null,
    batch: null,
    price: null,
    scale: null,
    margin: null,
    store: null,
    produceDatetime: null,
    expireTime: null,
    drugType: null,
    location: null,
    drugDesc: null,
    func: null,
    disease: null
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询【请填写功能名称】列表 */
function getList() {
  loading.value = true;
  listDrug(queryParams.value).then(response => {
    drugList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    drugName: null,
    provider: null,
    batch: null,
    price: null,
    scale: null,
    margin: null,
    store: null,
    produceDatetime: null,
    expireTime: null,
    drugType: null,
    location: null,
    drugDesc: null,
    remark: null,
    func: null,
    disease: null
  };
  proxy.resetForm("drugRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "新增药品";
}

/** 修改按钮操作 */
async function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  await getDrug(_id).then(response => {
    form.value = response.data;
    console.log(form.value)
    open.value = true;
    title.value = "修改";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["drugRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateDrug(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addDrug(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除【请填写功能名称】编号为"' + _ids + '"的数据项？').then(function() {
    return delDrug(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/drug/export', {
    ...queryParams.value
  }, `drug_${new Date().getTime()}.xlsx`)
}

getList();
</script>
